package com.cosina.game.tank.model;

import java.util.LinkedList;
import java.util.List;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.graphics.Canvas;
import android.graphics.Point;
import android.graphics.Rect;
import android.view.SurfaceHolder;

import com.cosina.game.tank.Constants;
import com.cosina.game.tank.TankGame;
import com.cosina.game.tank.model.impl.EnemyTank;
import com.cosina.game.tank.model.impl.JusticeTank;
import com.cosina.game.tank.view.WarDrawer;
import com.cosina.game.tank.view.WarView;

public abstract class War extends Thread implements TimeElapse{
	
	
	public boolean run = true;
	protected final SurfaceHolder holder;
	public final WarView warField;
	
	public final LinkedList<EnemyTank> enemyTanks = new LinkedList<EnemyTank>();
	public JusticeTank justiceTank;
	public final List<Pill> pills = new LinkedList<Pill>();
	private UpdateEngine upEngine;
	
	public abstract int getPrepare();
	public War(WarView warField, SurfaceHolder holder) {
		this.warField = warField;
		this.holder = holder;
		upEngine = new UpdateEngine(this);
	}
	
	public void tryMove(Tank tank,int xSpn, int ySpan){
		tank.offset(xSpn, ySpan);
		if(testIsReasonableLocation(tank) == false){
			tank.offset(-xSpn, -ySpan);
		}
	}
	
	
	
	
	private boolean testIsReasonableLocation(Tank tank){
		Rect aimRect = tank.getArea();
		if(Constants.area.contains(aimRect) == false){
			return false;
		}
		for(EnemyTank each : enemyTanks){
			if(each == tank)
				continue;
			if(each.getArea().intersect(aimRect)){
				return false;
			}
		}
		if(tank == justiceTank)
			return true;
		return justiceTank.getArea().intersect(aimRect) == false;
	}
	
	public boolean isTileEmpty(Point tile){
		Rect tileRect = new Rect(tile.x*32, tile.y*32, (tile.x+1)*32, (tile.y+1)*32);
		for(EnemyTank each : enemyTanks){
			if(each.getArea().intersect(tileRect)){
				return false;
			}
		}
		return justiceTank.getArea().intersect(tileRect) == false;
	}
	
	
	@Override
	public void run(){
		while(run){
			long timeCurrent = System.currentTimeMillis();
			upEngine.withTimeGo();
			long internal = 40 - System.currentTimeMillis() + timeCurrent;
			if(internal > 0){
				try {
					sleep(internal);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			
			Canvas canvas = holder.lockCanvas();
			WarDrawer.draw(canvas,this);
			holder.unlockCanvasAndPost(canvas);
			
		}
		warField.postDelayed(runable, 500);
		
	}
	
	Runnable runable = new Runnable(){
		public void run(){
			AlertDialog.Builder builder = new AlertDialog.Builder(warField.getContext());
			builder.setTitle("GAME OVER");
			builder.setMessage("Click To Start New Game");
			builder.setNeutralButton("Click", new OnClickListener(){

				@Override
				public void onClick(DialogInterface dialog, int which) {
					TankGame tg = (TankGame)warField.getContext();
					tg.startGame();
				}});
			builder.show();
		}
	};
}
